package cn.lau.common.security;

import cn.lau.auth.entity.SysRole;
import cn.lau.auth.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

public class SecurityUser extends SysUser implements UserDetails{


    public SecurityUser(SysUser sysUser) {

        if(sysUser!=null){
            this.setId(sysUser.getId());
            this.setAccountName(sysUser.getAccountName());
            this.setNickName(sysUser.getNickName());
            this.setPassword(sysUser.getPassword());
            this.setSysRoles(sysUser.getSysRoles());
        }

    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {


        System.out.println("getAuthorities()");
        //权限列表
        Collection<GrantedAuthority> authorities=new ArrayList<>();
        Set<SysRole> userRoles=this.getSysRoles();
        if(userRoles!=null){
            for(SysRole role:userRoles){
                //将权限，塞入权限列表
                SimpleGrantedAuthority authority=new SimpleGrantedAuthority(role.getName());
                authorities.add(authority);
            }
        }

        return authorities;
    }

    @Override
    public String getUsername() {
        return super.getAccountName();
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
